package nl.hengelsportmolenberg.boilies.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import nl.hengelsportmolenberg.boilies.client.BoiliesService;
import nl.hengelsportmolenberg.boilies.shared.CustomerCard;
import nl.hengelsportmolenberg.boilies.shared.OrderDetails;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class BoiliesServiceImpl extends RemoteServiceServlet implements BoiliesService {
	
	private static final int[] amounts = new int[] {
		45, 60, 150, 2070, 13
	};
	
	Date now = new Date();
	
	private static final Date[] dates = new Date[] {
		new Date(), new Date(), new Date(), new Date(), new Date()
	};
	
	private int customerId = 1;
	private String givenName = "Alvin";
	private String familyName = "Kwekel";
	
	private final HashMap<Integer, OrderDetails> orders = new HashMap<Integer, OrderDetails>();

	
	public CustomerCard getCustomerCard(int id) {
		System.out.println("getCustomerCard called");
		CustomerCard customerCard = new CustomerCard();
	    for (int i = 0; i < amounts.length && i < dates.length; ++i) {
	        OrderDetails order = new OrderDetails(i, amounts[i], dates[i]);
	        orders.put(order.getId(), order);
	    }
	    ArrayList<OrderDetails> orderList = new ArrayList<OrderDetails>();
	    Iterator<Integer> it = orders.keySet().iterator();
	    while (it.hasNext()) {
	    	orderList.add(orders.get(it.next()));
	    }
	    customerCard.setOrderList(orderList);
	    customerCard.setGivenName(givenName);
	    customerCard.setFamilyName(familyName);
	    customerCard.setId(customerId);
		return customerCard;

	}
	
}
